package com.ck.juc;

import lombok.AllArgsConstructor;
import lombok.SneakyThrows;

public class DeadLock{

    public static void main(String[] args) {
        Integer a = new Integer("1");
        Integer b = new Integer("2");
        S source = new S(a,b);
        S s = new S(a,b);
        new Thread(new A(source),"AAAA").start();
        new Thread(new B(s),"BBBB").start();
    }
}
@AllArgsConstructor
class A implements Runnable{

    private S source;

    @Override
    public void run() {
        source.ajj();
    }
}
@AllArgsConstructor
class B implements Runnable {

    private S source;

    @Override
    public void run() {
        source.bjj();
    }
}

@AllArgsConstructor
class S {

    private Integer a;

    private Integer b;


    @SneakyThrows
    public void ajj(){
        synchronized (a){
            a++;
            System.out.println(Thread.currentThread().getName() + "\t执行完i++");
            Thread.sleep(1000);
            synchronized (b){
                b++;
            }
        }
    }

    @SneakyThrows
    public void bjj(){
        synchronized (b){
            b++;
            System.out.println(Thread.currentThread().getName() + "\t执行完b++");
            Thread.sleep(1000);
            synchronized (a){
                a++;
            }
        }
    }
}
